// pages/distribution/distribution_center.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    uid: '',
    avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
    nickName: '点击显示微信头像',
    if_register: false,
    if_show_mask: false,
    shopfinance_data: {},
    myinfo_data: {},
    eshop_id: 0,
    if_show_promotion_code: false,
    fanslist: [],
    recommend_Id: 0,
    recommend_name: '',
    my_name: '',
    distribution_icon_list: [{
      id: 1,
      title: '分销佣金',
      icon: '/images/commision_color.png',
      num: 20.95,
      targetUrl: '/pages/distribution/commition?index=1',
      distribution_code: '',
    },
    {
      id: 4,
      title: '我的团队',
      icon: '/images/team_blue.png',
      num: 22,
      targetUrl: '/pages/distribution/mymember?index=4'
    },
    {
      id: 5,
      title: '推广二维码',
      icon: '/images/acode_orange.png',
      num: '',
      targetUrl: '/pages/promoacode/promoacode?index=5'
    },
    {
      id: 7,
      title: '我的门店',
      icon: '/images/shareholder_green.png',
      num: '',
      targetUrl: '/pages/distribution/myshop'
    },
    {
      id: 8,
      title: '我的分红',
      icon: '/images/shareholder_green.png',
      num: '',
      targetUrl: '/pages/distribution/myincome'
    },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function(options) {
    this.init_data(options)
    this.get_myinfo();
    this.get_distribution_code();
    this.get_fanslist();
    this.get_recommend_name();
  },

  init_data(options) {
    let uid = util.getAppValue(App.globalData.uid)
    let eshop_id = App.globalData.eshop_id

    if (!uid) {
      uid = 1
    }
    this.setData({
      if_register: App.globalData.if_register,
      uid,
      options,
      eshop_id,
    })
    let avatarUrl = App.globalData.userInfo.avatarUrl
    let nickName = App.globalData.userInfo.nickName
    if (avatarUrl) {
      this.setData({
        avatarUrl,
        nickName,
      })
    }
  },

  get_myinfo() {
    let params = {
      uid: this.data.uid,
    }
    request.post(api.myinfo, params).then(res => {
      this.setData({
        myinfo_data: res,
        recommend_Id: res.recommend_id,
        my_name: res.true_name
      })
    })
  },

  async get_recommend_name() {
    console.log(this.data.recommend_Id)
    const that = this
    let params = {
      uid: that.data.recommend_Id,
    }
    request.post(api.myinfo, params).then(res => {
      this.setData({
        recommend_name: res.true_name,
      })
    })
  },

  check_register() {
    if (!this.data.if_register) {
      this.get_user_profile()
    }
  },

  show_promotion_code() {
    this.setData({
      if_show_mask: true,
      if_show_promotion_code: true,
    })
  },

  go_distribution_url(e) {
    let url = e.currentTarget.dataset.url
    // console.log(111);
    // if(url=='/pages/promoacode/promoacode?index=5'){
    //      this.get_acode_list()
    //     console.log(this.data.acode_list);
    //     return
    // }
    wx.navigateTo({
      url,
    })
  },

  go_withdraw() {
    wx.navigateTo({
      url: '/pages/distribution/withdraw',
    })
  },

  get_user_profile() {
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: res => {
        App.globalData.userInfo = res.userInfo
        App.globalData.if_register = true
        this.init_data(this.data.options)
        // let pages = getCurrentPages()
        // let prev_page = pages[pages.length - 2]
        // prev_page.onLoad()
        // util.goBack()
      },
      fail: res => {
        console.log(res);
      }
    })
  },

  clear_mask() {
    this.setData({
      if_show_mask: false,
      if_show_promotion_code: false,
    })
  },

  get_distribution_code() {
    let id = 10
    let ctype = 6
    let params = {
      uid: this.data.uid,
      id,
      ctype, 
    }
    request.post(api.qr_code, params).then(res => {
      this.setData({
        distribution_code: res.img
      })
    })
  },

  get_fanslist() {
    let params = {
      uid: this.data.uid
    }
    request.post(api.fanslist, params).then(res => {
      this.setData({
        fanslist: res
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})